layui.use(['table', 'admin', 'laypage', 'laytpl', 'tree', 'form', 'api', 'view', 'laydate', 'utill'], function () {
    var $ = layui.$,
        admin = layui.admin,
        view = layui.view,
        api = layui.api,
        table = layui.table,
        setter = layui.setter,
        laytpl = layui.laytpl,
        utill = layui.utill,
        tree = layui.tree,
        laydate = layui.laydate,
        form = layui.form;
    $('.screen').click(function () {
        $(this).attr('lay-href', '/home/paymentAccountForm');
    });
    var _TPL = {
        fiaacti: function (d) {
            if(d.fiaacti == '' || d.fiaacti == undefined){
                return "<span></span>";
            }else if(d.fiaacti != '' || d.fiaacti != undefined){
                if (d.fiaacti == 0) {
                    return "<span>一般</span>";
                } else if (d.fiaacti == 1) {
                    return "<span>良好</span>";
                } else if (d.fiaacti == 2) {
                    return "<span>不稳定</span>";
                } else if (d.fiaacti == 3) {
                    return "<span>折旧中</span>";
                } else if (d.fiaacti == 4) {
                    return "<span>外送</span>";
                } else if (d.fiaacti == 5) {
                    return "<span>折毕</span>";
                } else if (d.fiaacti == 6) {
                    return "<span>出售</span>";
                } else if (d.fiaacti == 7) {
                    return "<span>报废</span>";
                } else if (d.fiaacti == 8) {
                    return "<span>折毕再提</span>";
                } else if (d.fiaacti == 9) {
                    return "<span>改良</span>";
                }else if (d.fiaacti == 'A') {
                    return "<span>重估</span>";
                }
            }
        },
    };
    const cols = [[{
        type: 'checkbox',
        align: 'center',
    }, {
        type: 'numbers',
        title: '序号',
        align: 'center',
    }, {
        field: 'fia011',
        title: '资产编号'
    }, {
        field: 'fia01',
        title: '设备编号'
    }, {
        field: 'mover',
        title: '设备调机人员'
    }, {
        field: 'tcFtyName',
        title: '厂区'
    }, {
        field: 'tcProcName',
        title: '站别/工序'
    },
    {
        field: 'fiaacti',
        title: '设备状态',
        template: '#list-one'
    }]];
    const cols2 = [[{
        type: 'checkbox',
    }, {
        type: 'numbers',
        title: '序号',
    }, {
        field: 'fia011',
        title: '资产编号',
    }, {
        field: 'fia01',
        title: '设备编号',
    }, {
        field: 'mover',
        title: '设备调机人员',
    }, {
        field: 'tcFtyName',
        title: '厂区',
    }, {
        field: 'tcProcName',
        title: '站别/工序',
        unresize: false
    }, {
        field: '',
        title: ' <div class="layui-icon-made"></div>',
        type: 'normal',
        unresize: false,
        align: 'right'
    }]];
    utill.columnCustom('#table', 'FiaFileListVO', {
        url: api.host + api.sbll.label,
        templet: _TPL,
        done:function(res,curr,count){
            var pageNum = curr;
            var pageSize = res.data.length;
            admin.req({
                type: 'post',
                url: api.host + api.sbll.left_nav,
                success: function (data) {
                    var odata = data.data
                    function transferKey(odata) {
                        odata.forEach(obj => {
                            obj.title = obj.fic02
                            obj.children = obj.child
                            obj.id = obj.tcFtyId
                            delete obj["tcCatgName"]
                            delete obj["child"]
                            if (obj.children instanceof Array) {
                                transferKey(obj.children)
                            }
                        })
                        return odata
                    };
                    odataArr = transferKey(odata)
                    var inst1 = tree.render({
                        elem: '#tree', //传入元素选择器
                        showLine: false, //是否开启连接线
                        data: odataArr,
                        click: function (obj) {
                            var dom = $(obj.elem).parents('.layui-tree-set')[$(obj.elem).parents('.layui-tree-set').length - 1];
                            var pid = $(dom).data('id');
                            var object = new Array();
                            var id = new Object();
                            if (obj.data.tcFtyId != undefined) {
                                id.fia03 = obj.data.fic01;
                                id.fia14 = obj.data.tcFtyId;
                                id.keyWord = '';
                            } else {
                                id.fia03 = obj.data.fic01;
                                id.fia14 = pid;
                                id.keyWord = '';
                            }
                            object.push(id);
                            utill.columnCustom('#table', 'FiaFileListVO', {
                                url: api.host + api.sbll.render_table,
                                type: 'post',
                                where: id,
                                templet: _TPL
                            });

                        }
                    });
                    let html = `<i class="btn_icon layui-icon layui-icon-triangle-r"></i> `;
                    $('.tree').append(html);
                },

            });

        }
    });
             
    $('.search-blank-inner').focus(function () {
        $('.searchInput').css('width', '100px')
    });
    $('.search-blank-inner').blur(function () {
        $('.searchInput').css('width', '65px')
    });
  
    form.on('submit(query)', function (data) {
        utill.columnCustom('#table', 'FiaFileListVO', {
            url: api.host + api.sbll.label,
            where: data.field,
            templet: _TPL
        });
        return false;
    });
    utill.keyup('.searchInput','.search-blank-icon');
    table.on('tool(balance-table)', function (obj) {
        var index = $(this).attr('data-index');
        var id = {
            id: obj.data.tcDevId
        };
        sessionStorage.setItem('id', JSON.stringify(id));
        $(this).attr('lay-href', "/home/branch-one/detailed");
        var href = '[data-jump="home/balance"]';
        sessionStorage.setItem('layui-this', JSON.stringify(href));
    });
    function convertCanvasToImage(elem) {
        var img = '';
        elem.find('canvas').each(function (i, data) {
            var image = new Image();
            image.src = data.toDataURL("image/png");
            image.className = 'img' + i;
            var code = `.code` + i;
            $(code).append(image)
            $('#qrcode canvas').remove();
            return img = image;
        })
        return;
    }

    function makeQR(data) {
        
        layer.close(layer.index)
        data.reduce(function (per, current, index) {
            var str = JSON.stringify(current.fia01);
            var obj = str.replace('\"', "").replace('\"', "");
            var url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${api.appid}&redirect_uri=${api.redirect_uri}index.html%23%2Fqrcode%3Ffia01%3D${encodeURI(obj)}&response_type=code&scope=snsapi_base&agentid=1000021&state=1#wechat_redirect`;
            var html = `<div class='code${index} codeName'>
                        <h1>${current.fia02}</h1>
                        <h2>${current.fii02}</h2>
                    </div>`;
            $("#qrcode").append(html);
            $('.code' + index).qrcode({
                render: 'canvas',
                width: 200,
                height: 200,
                text: url
            });
        }, 0);
        convertCanvasToImage($('#qrcode'));
        layer.open({
            type: 1,
            title: false,
            closeBtn: false,
            btn: ['确认打印'],
            maxWidth: 700,
            maxHeight: 400,
            id: 'LAY_layuipro',
            move: false,
            content: $('#qrcode'),
            shadeClose: true,
            success: function (layero) {
                var mask = $(".layui-layer-shade");
                mask.appendTo(layero.parent());
                $(".layui-layer-shade").parent();
                $(".layui-layer-shade").parent();
            }, yes: function (index, layero) {
                $("#qrcode").jqprint({
                    debug: false,
                    importCSS: true,
                    printContainer: true,
                    operaSupport: false
                });
                layer.close(index);
            },
            end: function () {
                $('#qrcode').html('');
                $('#qrcode').css('display', 'none');
            }
        });
    }
    $('.footer').on('click', '.Printing', function () {
        var checkStatus = table.checkStatus('#table'); //idTest 即为基础参数 id 对应的值
        var data = checkStatus.data;
        var obj = {};
        if (checkStatus.data.length == '') {
            layer.msg('请选择您要打印的文件！')
        } else if (checkStatus.data.length == '1') {
            const data = checkStatus.data;
            makeQR(data);
        } else {
            const data = checkStatus.data;
            makeQR(data);
        }

    });
    var flg = true;
    $('#LAY_app_body').bind("click", function (e) {
        if ($(e.target).closest(".tree").size() <= 0) {
                $('.tree').css('min-width','0px')
                setTimeout(function(){
                    $('#tree').hide()
                },100)
            flg  = false
        }
    })
    $('.tree').on('click','.btn_icon',function (e){
        flg  = !flg
        if(flg == false){
            $('.tree').css('min-width','0px')
            setTimeout(function(){
                $('#tree').hide()
            },100)
        }else if(flg == true){
            setTimeout(function(){
                $('#tree').show()
            },250)
            $('.tree').css('min-width','150px')
        }
       
});
})